package arith;

import java.util.LinkedList;
import java.util.Random;

public class Factoring implements Operation, Input {
	
	int num;
	int[] factors;
	LinkedList<Integer> factorList;
	Problem prob;
	
	public Factoring(int n){
		num = n;
		prob = new Problem("<factor>");
		prob.setProblem("Find the factors of " + n + "");
		factorList = new LinkedList<Integer>();
		for(int i=1; i <= num; i++){
			if(num%i == 0){
				factorList.add(i);
			}
		}
		factors = new int[factorList.size()];
		for(int i=0; i<factors.length; i++){
			factors[i] = factorList.get(i);
		}
		prob.setSolution(factors);
	}
	
	public Factoring(int n1, int n2, int id){
		Random random = new Random();
		while(gcf(n1, n2) == 1){
			n1 = random.nextInt(99) + 1;
			n2 = random.nextInt(99) + 1;
		}
		if(id == 0){
			//GCF problem
			num = gcf(n1, n2);
			prob = new Problem("<gcf>");
			prob.setProblem("Find the greatest common factor of "+n1+" and "+n2);
			factors = new int[1];
			factors[0] = num;
		}
		if(id == 1){
			//LCM problem
			num = (n1*n2)/gcf(n1, n2);
			prob = new Problem("<lcm>");
			prob.setProblem("Find the least common multiple of "+n1+" and "+n2);
			factors = new int[1];
			factors[0] = num;
		}
	}

	private int gcf(int n, int d){
		if (n % d == 0){
			return d;
		}else if (d % n == 0){
            return n;
		}else{
            return gcf(n % d, d % n);
		}
    }
	
	public int id() {
		return 3;
	}

	public double value() {
		return 0;
	}
	
	public Problem getProblem() {
		return prob;
	}

}
